package com.li.pm.db;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import com.li.pm.R;

import android.content.Context;

public class InitDatabase {
	
	private Context context;
	
	//将res/raw中的城市数据库导入到安装的程序中的database目录下
    public void importInitDatabase() {
    	//数据库的目录
    	String dirPath="/data/data/com.li.pm/databases";
    	File dir = new File(dirPath);
    	if(!dir.exists()) {
    		dir.mkdir();
    	}
    	//数据库文件
    	File dbfile = new File(dir, "db_pmdatabase.db");
    	try {
    		if(!dbfile.exists()) {
    			dbfile.createNewFile();
    		}
    		//加载欲导入的数据库
    		InputStream is = context.getResources().openRawResource(R.raw.db_pmdatabase);
    		FileOutputStream fos = new FileOutputStream(dbfile);
    		byte[] buffere=new byte[is.available()];
    		is.read(buffere);
    		fos.write(buffere);
    		is.close();
    		fos.close();

    	}catch(FileNotFoundException  e){
    		e.printStackTrace();
    	}catch(IOException e) {
    		e.printStackTrace();
    	}
    }
}
